package demointerface;


class Money implements Cloneable {
    public double money = 12.5;
}
class Person implements Cloneable {
   public String name;
    public Money m;
    public Person(String name) {
        this.name = name;
         m = new Money();
    }

    @Override
   public Object clone() throws CloneNotSupportedException {//想要使用clone需要调用该方法
        Person tmp = (Person)super.clone();//拷贝
        tmp.m = (Money)this.m.clone();
        return tmp;
    }
}
public class Test2 {//克隆接口

    public static void main(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person("zhangsan");
        Person person2 = (Person) person1.clone();//拷贝 此时为浅拷贝
        System.out.println("修改前");
        System.out.println(person1.m.money);
        System.out.println(person2.m.money);
        person2.m.money = 99.99;
        System.out.println("修改后");
        System.out.println(person1.m.money);
        System.out.println(person2.m.money);
        //想要实现深拷贝 必须克隆出一个独立于对象的对象
    }
    public static void main1(String[] args) throws CloneNotSupportedException {
//        Person person1 = new Person("zhangsan");
//        Person person2 = (Person) person1.clone();//向下转型
//        System.out.println(person2.name);
    }
}
